<template>
  <div class="permission">
    <!-- 卡片 -->
    <el-card>
      <!-- 添加菜单的按钮 -->
      <el-row>
        <el-col style="text-align: right">
          <el-button type="primary" size="mini" @click="openadd(1, '0')"
            ><i class="el-icon-edit"></i>添加菜单</el-button
          >
        </el-col>
      </el-row>
      <!-- 菜单表格 -->
      <el-table :data="perList" row-key="id">
        <el-table-column label="菜单名称" prop="name"></el-table-column>
        <el-table-column label="权限标识" prop="code"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              v-if="row.type === 1"
              @click="openadd(2, row.id)"
              >添加权限点</el-button
            >
            <el-button type="text">查看 api 权限</el-button>
            <el-button type="text" @click="openDialog(row.id)">查看</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <mypanel
      ref="mypanel"
      :updateData="getPermissionAll()"
      :showDialog.sync="showDialog"
    ></mypanel>
  </div>
</template>

<script>
import { getPermissionAllApi, delPermissionApi } from '@/api/permission'
export default {
  data () {
    return {
      // 权限列表
      perList: [
        {
          id: '1',
          name: '企业管理',
          maker: 'saas-clients',
          dis: '企业管理',
          children: [
            {
              id: '1-1',
              name: '查看部门',
              maker: 'point-dept',
              dis: '查看部门'
            }
          ]
        },
        { id: '2', name: '员工管理', maker: 'employees', dis: '员工管理' }
      ],
      showDialog: false
    }
  },
  methods: {
    async getPermissionAll () {
      const res = await getPermissionAllApi()
      this.perList = this.tunDate(res.data, '0')
    },
    openadd (type, pid) {
      this.$refs.mypanel.setprop(type, pid)
      this.showDialog = true
    },
    tunDate (arr, pid) {
      const newarr = []
      arr.forEach(item => {
        if (item.pid === pid) {
          const children = this.tunDate(arr, item.id)
          if (children.length > 0) {
            item.children = children
          }
          newarr.push(item)
        }
      })
      return newarr
    },
    async del (id) {
      await this.$confirm('您确定删除吗?', '温馨提示')
      await delPermissionApi(id)
      this.$message.success('删除成功')
      this.getPermissionAll()
    },
    openDialog (id) {
      this.showDialog = true
      this.$refs.mypanel.getObjById(id)
    }
  },
  created () {
    this.getPermissionAll()
  },
  components: {
    mypanel: () => import('./components/panel.vue')
  }
}
</script>

<style lang="scss">
.permission {
  margin: 20px;
}
</style>
